home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / CHIP Turkiye Ekim 2000.iso / prog / naps / 04 / setup.exe / Gnucleus / HyperLink.cpp < prev    next >
C/C++ Source or Header  |  2000-06-17  |  15KB  |  495 lines

  1. // HyperLink.cpp : implementation file
  2. //
  3. // HyperLink static control. Will open the default browser with the given URL
  4. // when the user clicks on the link.
  5. //
  6. // Copyright (C) 1997 - 1999 Chris Maunder
  7. // All rights reserved. May not be sold for profit.
  8. //
  9. // Thanks to Pσl K. T°nder for auto-size and window caption changes.
  10. //
  11. // "GotoURL" function by Stuart Patterson
  12. // As seen in the August, 1997 Windows Developer's Journal.
  13. // Copyright 1997 by Miller Freeman, Inc. All rights reserved.
  14. // Modified by Chris Maunder to use TCHARs instead of chars.
  15. //
  16. // "Default hand cursor" from Paul DiLascia's Jan 1998 MSJ article.
  17. //
  18. // 2/29/00 -- P. Shaffer standard font mod.
  19.  
  20. #include "stdafx.h"
  21. #include "HyperLink.h"
  22.  
  23. #include "atlconv.h"    // for Unicode conversion - requires #include <afxdisp.h> // MFC OLE automation classes
  24.  
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30.  
  31. #define TOOLTIP_ID 1
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CHyperLink
  35.  
  36. CHyperLink::CHyperLink()
  37. {
  38.     m_hLinkCursor       = NULL;                 // No cursor as yet
  39.     m_crLinkColour      = RGB(  0,   0, 238);   // Blue
  40.     m_crVisitedColour   = RGB( 85,  26, 139);   // Purple
  41.     m_crHoverColour     = RGB(255,   0,   0);   // Red
  42.     m_bOverControl      = FALSE;                // Cursor not yet over control
  43.     m_bVisited          = FALSE;                // Hasn't been visited yet.
  44.     m_nUnderline        = ulHover;              // Underline the link?
  45.     m_bAdjustToFit      = TRUE;                 // Resize the window to fit the text?
  46.     m_strURL.Empty();
  47.     m_nTimerID          = 100;
  48. }
  49.  
  50. CHyperLink::~CHyperLink()
  51. {
  52.     m_UnderlineFont.DeleteObject();
  53. }
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CHyperLink overrides
  57.  
  58. BOOL CHyperLink::DestroyWindow() 
  59. {
  60.     KillTimer(m_nTimerID);
  61.     
  62.     return CStatic::DestroyWindow();
  63. }
  64.  
  65. BOOL CHyperLink::PreTranslateMessage(MSG* pMsg) 
  66. {
  67.     m_ToolTip.RelayEvent(pMsg);
  68.     return CStatic::PreTranslateMessage(pMsg);
  69. }
  70.  
  71.  
  72. void CHyperLink::PreSubclassWindow() 
  73. {
  74.     // We want to get mouse clicks via STN_CLICKED
  75.     DWORD dwStyle = GetStyle();
  76.     ::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
  77.     
  78.     // Set the URL as the window text
  79.     if (m_strURL.IsEmpty())
  80.         GetWindowText(m_strURL);
  81.  
  82.     // Check that the window text isn't empty. If it is, set it as the URL.
  83.     CString strWndText;
  84.     GetWindowText(strWndText);
  85.     if (strWndText.IsEmpty()) 
  86.     {
  87.         ASSERT(!m_strURL.IsEmpty());    // Window and URL both NULL. DUH!
  88.         SetWindowText(m_strURL);
  89.     }
  90.  
  91.     CFont* pFont = GetFont();
  92.     if (!pFont)
  93.     {
  94.         HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  95.         if (hFont == NULL)
  96.             hFont = (HFONT) GetStockObject(ANSI_VAR_FONT);
  97.         if (hFont)
  98.             pFont = CFont::FromHandle(hFont);
  99.     }
  100.     ASSERT(pFont->GetSafeHandle());
  101.  
  102.     // Create the underline font
  103.     LOGFONT lf;
  104.     pFont->GetLogFont(&lf);
  105.     m_StdFont.CreateFontIndirect(&lf);
  106.     lf.lfUnderline = (BYTE) TRUE;
  107.     m_UnderlineFont.CreateFontIndirect(&lf);
  108.  
  109.     PositionWindow();        // Adjust size of window to fit URL if necessary
  110.     SetDefaultCursor();      // Try and load up a "hand" cursor
  111.     SetUnderline();
  112.  
  113.     // Create the tooltip
  114.     CRect rect; 
  115.     GetClientRect(rect);
  116.     m_ToolTip.Create(this);
  117.     m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID);
  118.  
  119.     CStatic::PreSubclassWindow();
  120. }
  121.  
  122. BEGIN_MESSAGE_MAP(CHyperLink, CStatic)
  123.     //{{AFX_MSG_MAP(CHyperLink)
  124.     ON_WM_CTLCOLOR_REFLECT()
  125.     ON_WM_SETCURSOR()
  126.     ON_WM_MOUSEMOVE()
  127.     ON_WM_TIMER()
  128.     ON_CONTROL_REFLECT(STN_CLICKED, OnClicked)
  129.     ON_WM_ERASEBKGND()
  130.     //}}AFX_MSG_MAP
  131. END_MESSAGE_MAP()
  132.  
  133. /////////////////////////////////////////////////////////////////////////////
  134. // CHyperLink message handlers
  135.  
  136. void CHyperLink::OnClicked()
  137. {
  138.     m_bOverControl = FALSE;
  139.     int result = (int)GotoURL(m_strURL, SW_SHOW);
  140.     m_bVisited = (result > HINSTANCE_ERROR);
  141.     if (!m_bVisited)
  142.     {
  143.         MessageBeep(MB_ICONEXCLAMATION);     // Unable to follow link
  144.         ReportError(result);
  145.     }
  146.     else 
  147.         SetVisited();                        // Repaint to show visited colour
  148. }
  149.  
  150. HBRUSH CHyperLink::CtlColor(CDC* pDC, UINT nCtlColor) 
  151. {
  152.     ASSERT(nCtlColor == CTLCOLOR_STATIC);
  153.  
  154.     if (m_bOverControl)
  155.         pDC->SetTextColor(m_crHoverColour);
  156.     else if (m_bVisited)
  157.         pDC->SetTextColor(m_crVisitedColour);
  158.     else
  159.         pDC->SetTextColor(m_crLinkColour);
  160.  
  161.     // transparent text.
  162.     pDC->SetBkMode(TRANSPARENT);
  163.     return (HBRUSH)GetStockObject(NULL_BRUSH);
  164. }
  165.  
  166. void CHyperLink::OnMouseMove(UINT nFlags, CPoint point) 
  167. {
  168.     if (!m_bOverControl)        // Cursor has just moved over control
  169.     {
  170.         m_bOverControl = TRUE;
  171.  
  172.         if (m_nUnderline == ulHover)
  173.             SetFont(&m_UnderlineFont);
  174.         Invalidate();
  175.  
  176.         SetTimer(m_nTimerID, 100, NULL);
  177.     }
  178.     CStatic::OnMouseMove(nFlags, point);
  179. }
  180.  
  181. void CHyperLink::OnTimer(UINT nIDEvent) 
  182. {
  183.     CPoint p(GetMessagePos());
  184.     ScreenToClient(&p);
  185.  
  186.     CRect rect;
  187.     GetClientRect(rect);
  188.     if (!rect.PtInRect(p))
  189.     {
  190.         m_bOverControl = FALSE;
  191.         KillTimer(m_nTimerID);
  192.  
  193.         if (m_nUnderline != ulAlways)
  194.             SetFont(&m_StdFont);
  195.         rect.bottom+=10;
  196.         InvalidateRect(rect);
  197.     }
  198.     
  199.     CStatic::OnTimer(nIDEvent);
  200. }
  201.  
  202. BOOL CHyperLink::OnSetCursor(CWnd* /*pWnd*/, UINT /*nHitTest*/, UINT /*message*/) 
  203. {
  204.     if (m_hLinkCursor)
  205.     {
  206.         ::SetCursor(m_hLinkCursor);
  207.         return TRUE;
  208.     }
  209.     return FALSE;
  210. }
  211.  
  212. BOOL CHyperLink::OnEraseBkgnd(CDC* pDC) 
  213. {
  214.     CRect rect;
  215.     GetClientRect(rect);
  216.     pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));
  217.  
  218.     return TRUE;
  219. }
  220.  
  221. /////////////////////////////////////////////////////////////////////////////
  222. // CHyperLink operations
  223.  
  224. void CHyperLink::SetURL(CString strURL)
  225. {
  226.     m_strURL = strURL;
  227.  
  228.     if (::IsWindow(GetSafeHwnd())) {
  229.         PositionWindow();
  230.         m_ToolTip.UpdateTipText(strURL, this, TOOLTIP_ID);
  231.     }
  232. }
  233.  
  234. CString CHyperLink::GetURL() const
  235.     return m_strURL;   
  236. }
  237.  
  238. void CHyperLink::SetColours(COLORREF crLinkColour, COLORREF crVisitedColour,
  239.                             COLORREF crHoverColour /* = -1 */) 
  240.     m_crLinkColour    = crLinkColour; 
  241.     m_crVisitedColour = crVisitedColour;
  242.  
  243.     if (crHoverColour == -1)
  244.         m_crHoverColour = ::GetSysColor(COLOR_HIGHLIGHT);
  245.     else
  246.         m_crHoverColour = crHoverColour;
  247.  
  248.     if (::IsWindow(m_hWnd))
  249.         Invalidate(); 
  250. }
  251.  
  252. COLORREF CHyperLink::GetLinkColour() const
  253.     return m_crLinkColour; 
  254. }
  255.  
  256. COLORREF CHyperLink::GetVisitedColour() const
  257. {
  258.     return m_crVisitedColour; 
  259. }
  260.  
  261. COLORREF CHyperLink::GetHoverColour() const
  262. {
  263.     return m_crHoverColour;
  264. }
  265.  
  266. void CHyperLink::SetVisited(BOOL bVisited /* = TRUE */) 
  267.     m_bVisited = bVisited; 
  268.  
  269.     if (::IsWindow(GetSafeHwnd()))
  270.         Invalidate(); 
  271. }
  272.  
  273. BOOL CHyperLink::GetVisited() const
  274.     return m_bVisited; 
  275. }
  276.  
  277. void CHyperLink::SetLinkCursor(HCURSOR hCursor)
  278.     m_hLinkCursor = hCursor;
  279.     if (m_hLinkCursor == NULL)
  280.         SetDefaultCursor();
  281. }
  282.  
  283. HCURSOR CHyperLink::GetLinkCursor() const
  284. {
  285.     return m_hLinkCursor;
  286. }
  287.  
  288. void CHyperLink::SetUnderline(int nUnderline /*=ulHover*/)
  289. {
  290.     if (m_nUnderline == nUnderline)
  291.         return;
  292.  
  293.     if (::IsWindow(GetSafeHwnd()))
  294.     {
  295.         if (nUnderline == ulAlways)
  296.             SetFont(&m_UnderlineFont);
  297.         else
  298.             SetFont(&m_StdFont);
  299.  
  300.         Invalidate(); 
  301.     }
  302.  
  303.     m_nUnderline = nUnderline;
  304. }
  305.  
  306. int CHyperLink::GetUnderline() const
  307.     return m_nUnderline; 
  308. }
  309.  
  310. void CHyperLink::SetAutoSize(BOOL bAutoSize /* = TRUE */)
  311. {
  312.     m_bAdjustToFit = bAutoSize;
  313.  
  314.     if (::IsWindow(GetSafeHwnd()))
  315.         PositionWindow();
  316. }
  317.  
  318. BOOL CHyperLink::GetAutoSize() const
  319.     return m_bAdjustToFit; 
  320. }
  321.  
  322.  
  323. // Move and resize the window so that the window is the same size
  324. // as the hyperlink text. This stops the hyperlink cursor being active
  325. // when it is not directly over the text. If the text is left justified
  326. // then the window is merely shrunk, but if it is centred or right
  327. // justified then the window will have to be moved as well.
  328. //
  329. // Suggested by Pσl K. T°nder 
  330.  
  331. void CHyperLink::PositionWindow()
  332. {
  333.     if (!::IsWindow(GetSafeHwnd()) || !m_bAdjustToFit) 
  334.         return;
  335.  
  336.     // Get the current window position
  337.     CRect WndRect, ClientRect;
  338.     GetWindowRect(WndRect);
  339.     GetClientRect(ClientRect);
  340.  
  341.     ClientToScreen(ClientRect);
  342.  
  343.     CWnd* pParent = GetParent();
  344.     if (pParent)
  345.     {
  346.         pParent->ScreenToClient(WndRect);
  347.         pParent->ScreenToClient(ClientRect);
  348.     }
  349.  
  350.     // Get the size of the window text
  351.     CString strWndText;
  352.     GetWindowText(strWndText);
  353.  
  354.     CDC* pDC = GetDC();
  355.     CFont* pOldFont = pDC->SelectObject(&m_UnderlineFont);
  356.     CSize Extent = pDC->GetTextExtent(strWndText);
  357.     pDC->SelectObject(pOldFont);
  358.     ReleaseDC(pDC);
  359.  
  360.     // Adjust for window borders
  361.     Extent.cx += WndRect.Width() - ClientRect.Width(); 
  362.     Extent.cy += WndRect.Height() - ClientRect.Height(); 
  363.  
  364.     // Get the text justification via the window style
  365.     DWORD dwStyle = GetStyle();
  366.  
  367.     // Recalc the window size and position based on the text justification
  368.     if (dwStyle & SS_CENTERIMAGE)
  369.         WndRect.DeflateRect(0, (WndRect.Height() - Extent.cy)/2);
  370.     else
  371.         WndRect.bottom = WndRect.top + Extent.cy;
  372.  
  373.     if (dwStyle & SS_CENTER)   
  374.         WndRect.DeflateRect((WndRect.Width() - Extent.cx)/2, 0);
  375.     else if (dwStyle & SS_RIGHT) 
  376.         WndRect.left  = WndRect.right - Extent.cx;
  377.     else // SS_LEFT = 0, so we can't test for it explicitly 
  378.         WndRect.right = WndRect.left + Extent.cx;
  379.  
  380.     // Move the window
  381.     SetWindowPos(NULL, WndRect.left, WndRect.top, WndRect.Width(), WndRect.Height(), SWP_NOZORDER);
  382. }
  383.  
  384. /////////////////////////////////////////////////////////////////////////////
  385. // CHyperLink implementation
  386.  
  387. // The following appeared in Paul DiLascia's Jan 1998 MSJ articles.
  388. // It loads a "hand" cursor from the winhlp32.exe module
  389. void CHyperLink::SetDefaultCursor()
  390. {
  391.     if (m_hLinkCursor == NULL)                // No cursor handle - load our own
  392.     {
  393.         // Get the windows directory
  394.         CString strWndDir;
  395.         GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
  396.         strWndDir.ReleaseBuffer();
  397.  
  398.         strWndDir += _T("\\winhlp32.exe");
  399.         // This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
  400.         HMODULE hModule = LoadLibrary(strWndDir);
  401.         if (hModule) {
  402.             HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
  403.             if (hHandCursor)
  404.                 m_hLinkCursor = CopyCursor(hHandCursor);
  405.         }
  406.         FreeLibrary(hModule);
  407.     }
  408. }
  409.  
  410. LONG CHyperLink::GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
  411. {
  412.     HKEY hkey;
  413.     LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);
  414.  
  415.     if (retval == ERROR_SUCCESS) {
  416.         long datasize = MAX_PATH;
  417.         TCHAR data[MAX_PATH];
  418.         RegQueryValue(hkey, NULL, data, &datasize);
  419.         lstrcpy(retdata,data);
  420.         RegCloseKey(hkey);
  421.     }
  422.  
  423.     return retval;
  424. }
  425.  
  426. void CHyperLink::ReportError(int nError)
  427. {
  428.     CString str;
  429.     switch (nError) {
  430.         case 0:                       str = "The operating system is out\nof memory or resources."; break;
  431.         case SE_ERR_PNF:              str = "The specified path was not found."; break;
  432.         case SE_ERR_FNF:              str = "The specified file was not found."; break;
  433.         case ERROR_BAD_FORMAT:        str = "The .EXE file is invalid\n(non-Win32 .EXE or error in .EXE image)."; break;
  434.         case SE_ERR_ACCESSDENIED:     str = "The operating system denied\naccess to the specified file."; break;
  435.         case SE_ERR_ASSOCINCOMPLETE:  str = "The filename association is\nincomplete or invalid."; break;
  436.         case SE_ERR_DDEBUSY:          str = "The DDE transaction could not\nbe completed because other DDE transactions\nwere being processed."; break;
  437.         case SE_ERR_DDEFAIL:          str = "The DDE transaction failed."; break;
  438.         case SE_ERR_DDETIMEOUT:       str = "The DDE transaction could not\nbe completed because the request timed out."; break;
  439.         case SE_ERR_DLLNOTFOUND:      str = "The specified dynamic-link library was not found."; break;
  440.         case SE_ERR_NOASSOC:          str = "There is no application associated\nwith the given filename extension."; break;
  441.         case SE_ERR_OOM:              str = "There was not enough memory to complete the operation."; break;
  442.         case SE_ERR_SHARE:            str = "A sharing violation occurred. ";
  443.         default:                      str.Format(_T("Unknown Error (%d) occurred."), nError); break;
  444.     }
  445.     str = "Unable to open hyperlink:\n\n" + str;
  446.     AfxMessageBox(str, MB_ICONEXCLAMATION | MB_OK);
  447. }
  448.  
  449. HINSTANCE CHyperLink::GotoURL(LPCTSTR url, int showcmd)
  450. {
  451.     TCHAR key[MAX_PATH + MAX_PATH];
  452.  
  453.     // First try ShellExecute()
  454.     HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd);
  455.  
  456.     // If it failed, get the .htm regkey and lookup the program
  457.     if ((UINT)result <= HINSTANCE_ERROR) {
  458.  
  459.         if (GetRegKey(HKEY_CLASSES_ROOT, _T(".htm"), key) == ERROR_SUCCESS) {
  460.             lstrcat(key, _T("\\shell\\open\\command"));
  461.  
  462.             if (GetRegKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS) {
  463.                 TCHAR *pos;
  464.                 pos = _tcsstr(key, _T("\"%1\""));
  465.                 if (pos == NULL) {                     // No quotes found
  466.                     pos = _tcsstr(key, _T("%1"));      // Check for %1, without quotes 
  467.                     if (pos == NULL)                   // No parameter at all...
  468.                         pos = key+lstrlen(key)-1;
  469.                     else
  470.                         *pos = '\0';                   // Remove the parameter
  471.                 }
  472.                 else
  473.                     *pos = '\0';                       // Remove the parameter
  474.  
  475.                 lstrcat(pos, _T(" "));
  476.                 lstrcat(pos, url);
  477.  
  478.                 USES_CONVERSION;
  479.                 result = (HINSTANCE) WinExec(T2A(key),showcmd);
  480.             }
  481.         }
  482.     }
  483.  
  484.     return result;
  485. }
  486.  
  487.